<script lang="ts">
    export default { 
        name: "XtxCity"
    }
</script>
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
import type { CityItem, SelectCity } from '@/types/data'
import axios from 'axios'
// 定义一个变量：用来控制城市结构的显示与隐藏
const active = ref(false)
//定义一个方法 用来修改 active 的状态
const toggle = () => {
  active.value = !active.value
}
const target = ref( null )
//定义一个点击事件(给除了 target 之外的结构添加点击事件)
onClickOutside(target,() => {
  active.value = false
})
//定义一个变量, 用来保存选择的地址
const selectCity = {} as SelectCity
//定义一个变量 用来保存城市的数据
const cityList = ref<CityItem[]>([])
//定义一个变量,用来保存城市数据的备份数据
const cacheList = ref<CityItem[]>([])
//定义一个方法获取城市的数据
const getCityList = async () => {
  const res = await axios.get<CityItem[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  cityList.value = res.data
  //保存缓存的数据
  cacheList.value = res.data
}
getCityList()
//接收地址信息
defineProps<{
  address: string
}>()
//一旦面板关闭, 需要将数据重新设置为省
watch(active, (value) => {
  if (!value) {
    //重置数据源
    cityList.value = cacheList.value
  }
})
//提交到父组件的方法
const emits = defineEmits<{
  (e: 'changeCity', value: SelectCity) : void
}>()
//修改选择的城市
const changeCity = (v: CityItem) => {
  //判断当前操作的是哪一段
  if (v.level === 0) {
    // 省
    cityList.value = v.areaList //得到下面的城市
    selectCity.provinceCode = v.code
    selectCity.provinceName = v.name
  }
  if (v.level === 1) {
    //市
    cityList.value = v.areaList //得到下面的区/县
    selectCity.cityCode = v.code
    selectCity.cityName = v.name
  }
  if (v.level === 2 ) {
    //区/县( 关闭面板 )
    active.value = false
    selectCity.countyCode = v.code
    selectCity.countyName = v.name
    emits('changeCity', selectCity)
    
  }
}
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle"  :class="{ active: active }">
      <span class="placeholder" v-if="!address">请选择配送地址</span>
      <span class="placeholder" v-else> {{ address }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span @click="changeCity(item)" class="ellipsis" v-for="item in cityList" :key="item.name">{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
